#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>

const static char *filename = "./test.txt";
const static char *infofilename = "./info.txt";
const static char *studentInfofilename = "./studentInfo.txt";

void open()
{
    FILE *fp = fopen(filename, "w");
    if (fp == NULL)
    {
        perror("fopen failed!");
    }
    fclose(fp);
    fp = NULL;
}

void writeCharacter()
{
    FILE *fp = fopen(filename, "w");
    if (fp == NULL)
    {
        perror("fopen failed!");
    }
    for (int i = 0; i < 26; ++i)
    {
        putc('a' + i, fp);
    }
    fclose(fp);
    fp = NULL;
}

void readCharacter()
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("fopen failed!");
    }
    char buffer[27];
    fgets(buffer, 27, fp);
    printf("%s\n", buffer);
    fclose(fp);
    fp = NULL;
}

struct info
{
    int _age;            // 年纪
    const char *_gender; // 性别
    const char *_name;   // 姓名
};

void infoInit(struct info *info)
{
    info->_age = 23;
    info->_gender = "female";
    info->_name = "lisi";
}

void writeFormat(struct info info)
{
    FILE *fp = fopen(infofilename, "w");
    if (fp == NULL)
    {
        perror("fopen failed!");
        return;
    }
    int size = fprintf(fp, "%s %d %s", info._name, info._age, info._gender);
    if (size < 0)
    {
        printf("errno:%d, strerror:%s\n", errno, strerror(errno));
        return;
    }

    fclose(fp);
    fp = NULL;
}

void readFormat()
{
    FILE *fp = fopen(infofilename, "r");
    if (fp == NULL)
    {
        perror("fopen failed!");
        return;
    }
    struct info ret;
    ret._name = (char *)malloc(sizeof(char) * 10);
    ret._gender = (char *)malloc(sizeof(char) * 10);
    fscanf(fp, "%s %d %s", ret._name, &(ret._age), ret._gender);
    printf("name:%s, age:%d, gender:%s\n", ret._name, ret._age, ret._gender);
    fclose(fp);
    free((void *)ret._gender);
    free((void *)ret._name);
    fp = NULL;
}

struct studentInfo
{
    char _name[20];
    int _age;
    float _score;
};

void write()
{
    studentInfo stu = {"lisi", 19, 89.5};
    FILE *fp = fopen(studentInfofilename, "w");
    if (fp == NULL)
    {
        perror("fopen failed!");
        return;
    }
    fwrite(&stu, sizeof(studentInfo), 1, fp);
    fclose(fp);
    fp = NULL;
}

void read()
{
    studentInfo stu = {0};
    FILE *fp = fopen(studentInfofilename, "r");
    if (fp == NULL)
    {
        perror("fopen failed!");
        return;
    }
    fread(&stu, sizeof(studentInfo), 1, fp);
    printf("name:%s, age:%d, score:%f\n", stu._name, stu._age, stu._score);
    fclose(fp);
    fp = NULL;
}

void StringStream()
{
    studentInfo stu = {"lisi", 19, 89.5};
    char buffer[100] = {0};
    sprintf(buffer, "%s %d %f", stu._name, stu._age, stu._score);
    studentInfo ret = {0};
    sscanf(buffer, "%s %d %f", ret._name, &ret._age, &ret._score);
    printf("%s %d %f\n", ret._name, ret._age, ret._score);
}

void randomRead()
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("fopen failed!");
        return;
    }
    fseek(fp, 3, SEEK_SET);
    int character = fgetc(fp);
    printf("%c\n", character);

    printf("%d\n", ftell(fp));
    rewind(fp);
    printf("%d\n", ftell(fp));
}

int main()
{
    // open();
    // writeCharacter();
    // readCharacter();

    // struct info info;
    // infoInit(&info);
    // writeFormat(info);
    // readFormat();

    // write();
    // read();

    // StringStream();

    randomRead();

    return 0;
}
